package com.xjj.servicejob.controller.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xjj.entity.*;
import com.xjj.servicejob.feign.KlassFeignClient;
import com.xjj.servicejob.service.JobService;
import com.xjj.servicejob.service.JobSubmitService;
import com.xjj.servicejob.vo.JobSubmitDetailResultVo;
import com.xjj.servicejob.vo.JobSubmitVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import util.GetCurrentTime;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author sjy
 * @create 2022-10-20 19:27
 * 提交作业控制器
 */
@RestController
@RequestMapping("/job/api/jobSubmit")
public class JobSubmitController {
    @Autowired
    private JobSubmitService jobSubmitService;

    @Autowired
    private JobService jobService;

    @Autowired
    private KlassFeignClient klassFeignClient;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 提交作业
     *
     * @param jobSubmit
     * @return
     */
    @PostMapping("add")
    public ResponseEntity add(@RequestBody JobSubmit jobSubmit) {
        jobSubmitService.add(jobSubmit);
        return ResponseEntity.SUCCESS("ok", "成功提交作业");
    }

    /**
     * 修改作业(只能修改图片）
     *
     * @param jobSubmit
     * @return
     */
    @PostMapping("update")
    public ResponseEntity update(@RequestBody JobSubmit jobSubmit) {
        //判断是否修改图片
        if (jobSubmit.getJobSubmitPhotos() != null && jobSubmit.getJobSubmitPhotos().size() != 0) {
            jobSubmit.getJobSubmitPhotos().forEach(img -> {
                img.setRelationalId(jobSubmit.getId());
            });
            jobSubmitService.updateJobSubmitById(jobSubmit);
        }
        return ResponseEntity.SUCCESS("ok", "成功修改提交作业");
    }

    /**
     * 删除提交的作业（根据id）
     *
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    public ResponseEntity delete(@PathVariable Integer id) {
        jobSubmitService.deleteById(id);
        return ResponseEntity.SUCCESS("ok", "成功删除提交作业");
    }

    /**
     * 根据id查询作业的详情
     *
     * @param id
     * @return
     */
    @GetMapping("detail/{id}")
    public ResponseEntity<JobSubmitDetailResultVo> detail(@PathVariable Integer id) {
        JobSubmit jobSubmit = jobSubmitService.getById(id);
        Job job = jobService.detail(jobSubmit.getJobId());
        ResponseEntity responseEntity = klassFeignClient.getManagerById(job.getManagerId());
        Object o = responseEntity.getData();
        String s = JSON.toJSONString(o);
        Manager manager = JSONObject.parseObject(s, Manager.class);
        job.setSubject(manager.getSubject());
        JobSubmitDetailResultVo jobSubmitResultVo = new JobSubmitDetailResultVo();
        jobSubmitResultVo.setJobSubmit(jobSubmit);
        jobSubmitResultVo.setJob(job);
        return ResponseEntity.SUCCESS("ok", jobSubmitResultVo);
    }

    /**
     * 多条件分页查询作业(根据科目和日期）
     *
     * @param jobSubmitVo
     * @return
     */
    @PostMapping("list")
    public ResponseEntity<PageBean<Job>> getPageBean(@RequestBody JobSubmitVo jobSubmitVo) {
        //查询学生所在班级的班级id
        ResponseEntity responseEntity = klassFeignClient.getStudentById(jobSubmitVo.getStudentId());
        Object o = responseEntity.getData();
        String s = JSON.toJSONString(o);
        Student student = JSONObject.parseObject(s, Student.class);
        Integer klassId = student.getKlassId();
        Job job1 = jobSubmitVo.getJobVo().getJob();
        job1.setKlassId(klassId);
        jobSubmitVo.getJobVo().setJob(job1);
        //符合条件的作业
        PageBean<Job> pageBean = new PageBean<>();
        pageBean = jobSubmitService.getByPage(jobSubmitVo.getJobVo(), 10);
        List<Job> jobs = pageBean.getData();
        //将该学生的作业和学科放入该作业中
        for (int i = 0; i < jobs.size(); i++) {
            Job job = jobs.get(i);
//            JobSubmit jobSubmit = jobSubmitService.getByStudentIdAndJobId(jobSubmitVo.getStudentId(), job.getId());
//            job.setJobSubmit(jobSubmit);
            ResponseEntity managerById = klassFeignClient.getManagerById(job.getManagerId());
            Object o1 = managerById.getData();
            String s1 = JSON.toJSONString(o1);
            Manager manager = JSONObject.parseObject(s1, Manager.class);
            job.setSubject(manager.getSubject());
        }
        pageBean.setData(jobs);
        return ResponseEntity.SUCCESS("ok", pageBean);
    }

    /**
     * 根据学生id查询该班级的所有任课老师id和所教授的科目
     *
     * @param id
     * @return
     */
    @GetMapping("getAllTeacherByStudentId/{id}")
    public ResponseEntity<List<Student>> getAllTeacherByStudentId(@PathVariable Integer id) {
        ResponseEntity responseEntity = klassFeignClient.getStudentById(id);
        List teachers = new ArrayList();
        if (responseEntity.getCode() == 200) {
            Map map = (Map) responseEntity.getData();
            Object o = redisTemplate.opsForValue().get("jobSubmit" + (Integer) map.get("klassId"));
            //先去查询缓存中是否有该班级的所有的授课老师信息
            if (o == null) {
                ResponseEntity studentsResponse = klassFeignClient.getAllTeacherByKlassId((Integer) map.get("klassId"));
                Object responseData = studentsResponse.getData();
                String s = JSON.toJSONString(responseData);
                teachers = JSONObject.parseObject(s, List.class);
                redisTemplate.opsForValue().set("jobSubmit" + (Integer) map.get("klassId"), teachers);
            } else {
                teachers = (List) o;
            }
        }
        return ResponseEntity.SUCCESS("ok", teachers);
    }

    /**
     * 获取今日作业
     *
     * @param studentId
     * @return
     */
    @GetMapping("getTodayAllJob/{studentId}")
    public ResponseEntity<List<Job>> getTodayAllJob(@PathVariable Integer studentId) {
        ResponseEntity student = klassFeignClient.getStudentById(studentId);
        Object responseData = student.getData();
        String s1 = JSON.toJSONString(responseData);
        Student student1  = JSONObject.parseObject(s1, Student.class);
        Integer klassId = student1.getKlassId();
        //根据学生id查
        List<Job> jobs = jobService.getTodayAllJob(klassId);
        for (int i = 0; i < jobs.size(); i++) {
            Job job = jobs.get(i);
            JobSubmit jobSubmit = jobSubmitService.getByStudentIdAndJobId(studentId, job.getId());
            job.setJobSubmit(jobSubmit);
            ResponseEntity responseEntity = klassFeignClient.getManagerById(job.getManagerId());
            Object o = responseEntity.getData();
            String s = JSON.toJSONString(o);
            Manager manager = JSONObject.parseObject(s, Manager.class);
            job.setSubject(manager.getSubject());
        }
        return ResponseEntity.SUCCESS("ok", jobs);
    }
}
